package com.spring.cardapi.web.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import com.spera.genutil.TokenUtil;
import com.spring.cardapi.web.dao.CardApiMapper;
import com.spring.cardapi.web.dao.ContractMapper;
import com.spring.cardapi.web.model.Consume;
import com.spring.cardapi.web.model.MyCoupon;
import com.spring.cardapi.web.model.Order;
import com.spring.cardapi.web.model.PointBill;
import com.spring.cardapi.web.service.CardApiService;
import com.spring.cardapi.web.service.ContractService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.core.util.UpdateMap;
import com.spring.security.web.dao.CommonMapper;
import com.spring.security.web.dao.JedisCache;
import com.spring.tools.MyTools;
import com.sso.api.web.dao.*;
import com.sso.api.web.model.RequestUser;
@Service
public class ContractServiceImpl implements  ContractService{
	@Resource
	private CardApiMapper cardApiMapper;
	
	@Resource
	private ContractMapper contractMapper;	
	
	@Resource
	private SsoMapper ssoMapper;
	
    @Resource
    private JedisCache jedisCache;
    
    @Resource
    private CommonMapper commonMapper;

	@Override
	public Map<String, Object> getContractList(String token,String contractTypeCode,String pageSize, String pageIndex, String skip, String rows) throws Exception{
		Map<String, Object> m = new HashMap<String, Object>();
		
		Map<String, Object> t = TokenUtil.checkToken(token,jedisCache);
		if (t != null) {
			return t;
		}

		String c_user_id = jedisCache.hget(token, TokenUtil.USER_ID);
		
		HashMap<String, Object> hm=MyTools.pageIndexToSkip(pageSize, pageIndex, skip, rows);
				
		hm.put("c_user_id", c_user_id);
		hm.put("contractTypeCode", contractTypeCode);
		
		List<HashMap<String, Object>> list=contractMapper.getContractList(hm);
			
		m.put(ApplicationUtils.DATASET, list);
		m.put(ApplicationUtils.STATUS, "0");
		m.put(ApplicationUtils.MSG, "插入失败");

		return m;
		
	}
	
	
	@Override
	public Map<String, Object> deleteContract(String contractID) throws Exception{
		Map<String, Object> m = new HashMap();
		
		HashMap hmc=MyTools.checkIsNotNull(contractID);
		if(hmc!=null){
			return hmc;
		}
		
		HashMap<String, Object> contract = contractMapper.getContract(contractID);
		
		String contractType=MyTools.ObjectToString(contract.get("contractTypeCode"));
		String EntityAvailableState=MyTools.ObjectToString(contract.get("EntityAvailableState"));
			if(contractType==null){
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "请输入有效id");
				return m;
			}
			if(EntityAvailableState==null||EntityAvailableState.equals("20")){
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "该合同已被删除，请勿重复删除");
				return m;				
			}
			
			
			int count=contractMapper.getContractHasConsume(contractID);
			if(count>0){
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "该合同已经被消费，无法删除");
				return m;	
			}
			
			//删除合同
			UpdateMap u = new UpdateMap("contract");
	    	 u.addField("EntityAvailableState", "20");
	    	 u.addWhere("id", contractID);
	         int i = commonMapper.update(u);	
				if(i<1){
					throw new Exception();
				}
			
			if(contractType.equals("1")){
				UpdateMap membercard = new UpdateMap("membercard");
				membercard.addField("isenable", "0");
				membercard.addField("EntityAvailableState", "20");
				membercard.addWhere("contractID", contractID);
		         int i2 = commonMapper.update(membercard);	
					if(i2<1){
						throw new Exception();
					}
			}else if(contractType.equals("4")){
				UpdateMap rechargecard = new UpdateMap("rechargecard");
				rechargecard.addField("isenable", "0");
				rechargecard.addField("EntityAvailableState", "20");
				rechargecard.addWhere("contractID", contractID);
		         int i2 = commonMapper.update(rechargecard);	
					if(i2<1){
						throw new Exception();
					}
			}else{
				throw new Exception();
			}
			
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "删除成功");
		
		return m;

	}
}
